Page({
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [
      { name: '北京西三旗花园1号楼 101' },
      { name: '北京东村家园3号楼 302' },
      { name: '北京育新花园3号楼 703' },
      { name: '北京天通苑北苑8号楼 403' },
    ],
    repairItem: [{ name: '水路卫浴' }, { name: '电路灯具' }, { name: '管道疏通' }, { name: '开锁换锁' }],
    attachment: [],
    description: '',
    mobile: ''
  },
  onLoad({ id }) {
    this.getHouseList()
    this.getrepairItem()
    if (id) {
      wx.setNavigationBarTitle({
        title: '修改报修信息'
      })
      this.getRepairDetail(id)
    }
  },
  //获取查询报修详情信息
  async getRepairDetail(id) {
    const res = await wx.http.get(`/repair/${id}`)
    console.log(res);
    this.setData({
      ...res.data
    })
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  //选择报修房屋
  selectHouse(e) {
    const { name, id: houseId } = e.detail
    this.setData({
      houseInfo: name,
      houseId
    })
  },
  //选择报修项目
  repairItem(e) {
    const { name, id: repairItemId } = e.detail
    this.setData({
      repairItemName: name,
      repairItemId
    })
  },
  //选择日期时间
  selectTime(e) {
    const time = wx.utils.formatTime(e.detail)
    this.setData(
      {
        dateLayerVisible: false,
        appointment: time
      })
  },
  //图片上传
  afterRead(event) {
    const { file } = event.detail
    wx.uploadFile({
      url: 'https://live-api.itheima.net/upload', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      header: { Authorization: `Bearer ${getApp().token}` },
      success: (res) => {
        const { data } = JSON.parse(res.data)
        // 上传完成需要更新 attachment
        const { attachment = [] } = this.data
        attachment.push({ ...data })
        this.setData({ attachment })
      },
    });
  },
  //获取审核通过的房屋
  async getHouseList() {
    const { data } = await wx.http.get('/house')
    this.setData({
      houseList: data
    })
  },
  //获取维修项目-选项
  async getrepairItem() {
    const { data } = await wx.http.get('/repairItem')
    this.setData({
      repairItem: data
    })
  },
  //校验选择报修房屋
  checkSelectHouse() {
    const vaild = Boolean(this.data.houseId)
    if (!vaild) wx.utils.toast('请选择报修房屋')
    return vaild
  },
  //校验维修项目
  checkRepairItem() {
    const vaild = Boolean(this.data.repairItemId)
    if (!vaild) wx.utils.toast('请选择维修项目')
    return vaild
  },
  //校验手机号码
  checkMobile() {
    const reg = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/
    const vaild = reg.test(this.data.mobile)
    if (!vaild) wx.utils.toast('请输入合法的手机号码')
    return vaild
  },
  //校验预约日期
  checkDate() {
    const reg = /^\d{4}[-|/]\d{2}[-|/]\d{2}$/
    const vaild = reg.test(this.data.appointment)
    if (!vaild) wx.utils.toast('请选择预约日期')
    return vaild
  },
  //校验问题描述
  checkDesc() {
    const vaild = this.data.description
    if (!vaild) wx.utils.toast('请输入问题描述内容')
    return vaild
  },
  //校验问题附件
  checkAttachment() {
    const vaild = this.data.attachment.length > 0
    if (!vaild) wx.utils.toast('请上传问题附件')
    return vaild
  },
  //提交报修
  async onSubmit() {
    if (!this.checkSelectHouse()) return
    if (!this.checkRepairItem()) return
    if (!this.checkMobile()) return
    if (!this.checkDate()) return
    if (!this.checkDesc()) return
    if (!this.checkAttachment()) return
    const { id, houseId, repairItemId, mobile, appointment, description, attachment } = this.data
    await wx.http.post('/repair', { id, houseId, repairItemId, mobile, appointment, description, attachment })
    wx.utils.toast(this.data.id ? '修改报修成功' : '提交报修成功')
    wx.navigateTo({
      url: '/repair_pkg/pages/list/index',
    })
  }
})
